
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true
})

const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  // 开发环境配置
  lintOnSave: process.env.NODE_ENV === 'development',
  
  // CSS 配置
  css: {
    sourceMap: process.env.NODE_ENV === 'development',
    loaderOptions: {
      scss: {
        // additionalData: `@import "@/assets/styles/index.scss";`
      }
    },
    extract: process.env.NODE_ENV === 'production'
  },
  
  
  // Webpack 配置
  configureWebpack: config => {
    const commonConfig = {
      resolve: {
        extensions: ['.js', '.vue', '.json', '.ts', '.tsx'],
        alias: {
          '@': resolve('src'),
          '@components': resolve('src/components'),
          '@views': resolve('src/views'),
          '@assets': resolve('src/assets'),
          '@router': resolve('src/router'),
          '@utils': resolve('src/utils'),
          '@api': resolve('src/api')
        }
      }
    };
    return commonConfig;
  },
  
  // 开发服务器配置
  devServer: {
    host: '0.0.0.0',
    port: 8080,
    open: true,
    hot: true,
    compress: true,
    allowedHosts: 'all',
    historyApiFallback: true,
    // 网络配置
    // network: {
    //   useLocalIp: true,
    //   showNetworkInfo: true
    // },
    client: {
      overlay: {
        errors: true,
        warnings: false
      },
      progress: true,
      // 客户端网络配置
      webSocketURL: {
        hostname: '0.0.0.0',
        pathname: '/ws',
        port: 8080,
        protocol: 'ws'
      }
    },
    // 代理配置
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        },
        // 代理网络配置
        timeout: 5000,
        proxyTimeout: 5000,
        onProxyReq: (proxyReq, req, res) => {
          // 添加请求头
          proxyReq.setHeader('X-Forwarded-For', req.connection.remoteAddress);
          proxyReq.setHeader('X-Real-IP', req.connection.remoteAddress);
        },
        onProxyRes: (proxyRes, req, res) => {
          // 处理响应
          console.log(`代理请求: ${req.method} ${req.url} -> ${proxyRes.statusCode}`);
        },
        onError: (err, req, res) => {
          console.error('代理错误:', err);
        }
      },
      // 添加更多代理配置
      '/upload': {
        target: 'http://localhost:3001',
        changeOrigin: true,
        timeout: 10000
      },
      '/socket.io': {
        target: 'http://localhost:3002',
        ws: true,
        changeOrigin: true
      }
    },
    // 静态文件服务
    static: {
      directory: path.join(__dirname, 'public'),
      publicPath: '/',
      // 静态文件网络配置
      serveIndex: true,
      watch: {
        ignored: /node_modules/
      }
    },
    // 网络相关配置
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
      'Access-Control-Allow-Headers': 'X-Requested-With, content-type, Authorization'
    },
    // 网络超时设置
    // retry: {
    //   attempts: 3,
    //   delay: 1000
    // }
  }
};